package com.example.onlinemonitoring.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.model.Dtssensorandpartition;
import com.example.onlinemonitoring.model.Tabcaption;
import com.example.onlinemonitoring.model.common.Tabcaption_s;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.Nodeconfig;
import com.example.onlinemonitoring.model.common.pagebase;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.serviceImpl.TabcaptionServiceImpl;
import com.example.onlinemonitoring.dao.AppstationMapper;
import com.example.onlinemonitoring.dao.TabcaptionMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * Controller
 * 该部分代码为自动生成，请勿随意修改
 * 如有改动请删除该注释
 * 
 * @author little pig page
 * @date 2022-03-28
 */
@Api(value = "设备类别信息", tags = { "设备类别信息" })
@RestController
@RequestMapping("/Equipmentcategoryinformation")
public class TabcaptionController {
    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
    @Autowired
    TabcaptionServiceImpl tabcaptionServiceImpl;
    @Autowired
    TabcaptionMapper tabcaptionmapper;

    @ApiOperation(value = "添加设备参数", notes = "添加设备参数")
    @ResponseBody
    @RequestMapping(value = "/updatetable", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updatetable(@RequestBody String jsonParam,
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "tabid", required = true) Integer tabid,
            @RequestParam(value = "tablevel", required = false) Integer tablevel) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        Tabcaption record = object.toJavaObject(Tabcaption.class);
        MyResult<Integer> m = new MyResult<Integer>();
        Example example = new Example(Tabcaption.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        criteria.andEqualTo("tabId", tabid);
        // criteria.andEqualTo("tabname", record.getTabname());
        record.setTabId(tabid);
        record.setTabLevel(tablevel);
        // record.setTaborder(taborder);
        // record.setTabenable(tabenable);
        if (tabid == 0)
            return null;

        if (id != null)
            if (tabcaptionServiceImpl.updateByExampleSelective(record, example) >= 1) {
                // if(tabcaptionServiceImpl.Update(record,false) >= 1){
                m.setMsg("修改成功");
                m.setCode(0);
            } else {
                m.setMsg("插入数据");
                m.setCode(0);
                tabcaptionmapper.insertSelective(record);

            }
        else {
            m.setMsg("插入数据");
            m.setCode(0);
            tabcaptionmapper.insertSelective(record);

        }
        return m;
    }

    // @ApiOperation(value = "新增设备数据", notes = "新增设备数据")
    // @ResponseBody
    // @GetMapping("/AddData")
    // public MyResult<Integer> AddData(@RequestBody String jsonParam,
    // @RequestParam(value = "tabid", required = false) Integer tabid,
    // @RequestParam(value = "tabname", required = false) String tabname
    // ) {
    // JSONObject object = JSONObject.parseObject(jsonParam);
    // Tabcaption record = object.toJavaObject(Tabcaption.class);
    // // Tabcaption record = new Tabcaption();
    // // record.setStationid(stationid);
    // // record.setDeviceaddr(deviceaddr);
    // // record.setDevicegroup(devicegroup);
    // Example example = new Example(Tabcaption.class);
    // Example.Criteria criteria = example.createCriteria();
    // criteria.andEqualTo("tabid",tabid);
    // criteria.andEqualTo("tabname", tabname);
    // tabcaptionmapper.insertSelective(record);
    // MyResult<Integer> m = new MyResult<Integer>();
    // m.setMsg("插入数据");
    // m.setCode(0);
    // return m;
    // }
    @ApiOperation(value = "查询设备数据", notes = "查询设备数据")
    @ResponseBody
    @GetMapping("/selectTabData")
    public MyResult<Tabcaption_s> selectTabData(@RequestParam(value = "deviceaddr", required = false) String deviceaddr,
            @RequestParam(value = "pointaddr", required = false) String pointaddr,
            @RequestParam(value = "tabid", required = false) Integer tabid,
            @RequestParam(value = "tabType", required = false) Integer tabtype,
            @RequestParam(value = "modid", required = false) String modid,
            @RequestParam(value = "modname", required = false) String modname,
            @RequestParam(value = "tabname", required = false) String tabname) {
        // Tabcaption record = new Tabcaption();
        // record.setStationid(stationid);
        // record.setDeviceaddr(deviceaddr);
        // record.setDevicegroup(devicegroup);
        Example example = new Example(Tabcaption.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("deviceaddr", deviceaddr);
        criteria.andEqualTo("pointaddr", pointaddr);
        criteria.andEqualTo("tabId", tabid);
        criteria.andEqualTo("tabType", tabtype);
        criteria.andEqualTo("modid", modid);
        criteria.andEqualTo("modname", modname);
        criteria.andEqualTo("tabName", tabname);
        MyResult<Tabcaption> res = tabcaptionServiceImpl.getList(example);
        List<Tabcaption> r_s = res.getData();
        List<Tabcaption_s> tab_s = new ArrayList<Tabcaption_s>();
        for (int i = 0; i < r_s.size(); i++) {
            Tabcaption_s ts = new Tabcaption_s();
            Tabcaption tab = r_s.get(i);
            ts.setId(tab.getId());
            ts.setDeviceaddr(tab.getDeviceaddr());
            ts.setPointaddr(tab.getPointaddr());
            ts.setTabId(tab.getTabId());
            ts.setTabName(tab.getTabName());
            ts.setlabel(tab.getTabName());
            ts.setModid(tab.getModid());
            ts.setModname(tab.getModname());
            ts.setTabType(tab.getTabType());
            ts.setTabLevel(tab.getTabLevel());
            ts.setTabOrder(tab.getTabOrder());
            ts.setTabEnable(tab.getTabEnable());
            ts.setMemo(tab.getModname());
            ts.setlocal(tab.getLocal());
            tab_s.add(ts);
        }

        MyResult<Tabcaption_s> resnode = new MyResult<>();
        resnode.setCount(r_s.size());
        resnode.setData(tab_s);
        return resnode;
    }
    // @ApiOperation(value = "查询所有设备数据", notes = "查询所有设备数据")
    // @ResponseBody
    // @GetMapping("/selectAllData")
    // public MyResult<Nodeconfig> selectAllData(@RequestParam(value = "tabid",
    // required = false) Integer tabid,
    // @RequestParam(value = "tabname", required = false) String tabname
    // ) {
    // // Tabcaption record = new Tabcaption();
    // // record.setStationid(stationid);
    // // record.setDeviceaddr(deviceaddr);
    // // record.setDevicegroup(devicegroup);

    // Example example = new Example(Tabcaption.class);
    // Example.Criteria criteria = example.createCriteria();
    // criteria.andEqualTo("tabId",0);
    // criteria.andEqualTo("tabName", tabname);
    // MyResult<Tabcaption> res = tabcaptionServiceImpl.getList(example);
    // List<Tabcaption> ltab = res.getData();
    // List<Nodeconfig>root0= new ArrayList<Nodeconfig>();;
    // List<Nodeconfig> current= new ArrayList<Nodeconfig>();;
    // //root=null;
    // for(int i=0;i<ltab.size();i++)
    // {
    // Nodeconfig nd= new Nodeconfig(ltab.get(i));
    // root0.add(nd) ;
    // current.add(nd);
    // }
    // Example example1 = new Example(Tabcaption.class);
    // Example.Criteria criteria1 = example.createCriteria();
    // criteria.andEqualTo("tabId",tabid);
    // criteria.andEqualTo("tabName", tabname);
    // MyResult<Tabcaption> res1 = tabcaptionServiceImpl.getList(example);
    // for(int i=0;i<ltab.size();i++)
    // {
    // Nodeconfig newchild = new Nodeconfig(ltab.get(i));
    // Nodeconfig newnd = current.get(i);
    // newnd.children.add(newchild);
    // newnd=newchild;

    // }
    // MyResult<Nodeconfig> resnode= new MyResult<>();
    // resnode.setData(root0);
    // return resnode;
    // }
    @ApiOperation(value = "删除数据库表", notes = "删除数据库表")
    @ResponseBody
    @GetMapping("/deleteTable")
    public MyResult<Integer> deleteTable(@RequestParam(value = "id", required = false) Integer id,
            // @RequestParam(value = "tabid", required = false) Integer tabid,
            @RequestParam(value = "tabname", required = false) String tabname) {
        // Tabcaption record = new Tabcaption();
        // record.setDeviceaddr(deviceaddr);
        // record.setDevicegroup(devicegroup);
        // tabcaptionServiceImpl.Delete(record);
        Example example = new Example(Tabcaption.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        criteria.andEqualTo("tabName", tabname);
        // criteria.andEqualTo("deviceid", record.getId());
        // Example example = new Example(Tabcaption.class);
        // Example.Criteria criteria = example.createCriteria();
        // criteria.andEqualTo("tabId",tabid);
        // criteria.andEqualTo("tabName", tabname);
        MyResult<Integer> m = new MyResult<Integer>();
        MyResult<Tabcaption> res = tabcaptionServiceImpl.getList(example);
        List<Tabcaption> ltab = res.getData();
        for (int i = 0; i < ltab.size(); i++) {
            Tabcaption tab = ltab.get(i);
            if (tab.getTabId() == 0) {
                m.setMsg("删除失败");
                m.setCode(1);
                return m;
            }
        }

        {
            tabcaptionServiceImpl.deleteByExample(example);
            m.setMsg("删除成功");
            m.setCode(0);
        }

        return m;
    }
    // @ApiOperation(value="修改监测点名称", notes= "修改监测点名称")
    // @ResponseBody
    // @RequestMapping(value = "/updateMonitorName", method = RequestMethod.POST,
    // produces = "application/json;charset=UTF-8")
    // public MyResult<Integer> updateMonitorName(@RequestBody String jsonParam,
    // @RequestParam(value = "id", required = false) Long id
    // ) {
    // JSONObject object = JSONObject.parseObject(jsonParam);
    // MyResult<Integer> m = new MyResult<Integer>();
    // Tabcaption record = new Tabcaption();
    // record.setId(id);
    // record.setPointname(object.getString("pointname"));
    // if( tabcaptionServiceImpl.Update(record,true) >= 1){
    // m.setMsg("修改成功");
    // m.setCode(0);
    // }else{
    // m.setMsg("修改失败");
    // m.setCode(1);
    // }
    // return m;
    // }

    /**
     * 导出Excel
     */
    // @GetMapping("exportToExcel")
    // public void exportToExcel(HttpServletResponse response,
    // @RequestParam(value = "stationid", required = true) Integer stationid,
    // @RequestParam(value = "devicegroup", required = true) Integer devicegroup,
    // @RequestParam(value = "deviceaddr", required = true) Integer deviceaddr,
    // @RequestParam(value = "start", required = false) String begintime,
    // @RequestParam(value = "end", required = false) String endtime) throws
    // IOException {
    // Tabcaption record = new Tabcaption();
    // record.setStationid(stationid);
    // record.setDeviceaddr(deviceaddr);
    // record.setDevicegroup(devicegroup);
    // pagebase pagebase = new pagebase();
    // pagebase.setBegintime(begintime);
    // pagebase.setEndtime(endtime);
    // Workbook wb = tabcaptionServiceImpl.writeToExcelByList(record, pagebase);
    // OutputStream output = response.getOutputStream();
    // String fileName = "data.xlsx";
    // try {
    // fileName = URLEncoder.encode(fileName, "UTF-8");
    // } catch (UnsupportedEncodingException e) {
    // e.printStackTrace();
    // }
    // response.setHeader("Content-disposition",
    // "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
    // wb.write(output);
    // output.close();
    // }
    /**
     * 导出Excel
     */
    // @GetMapping("exportToSensorExcel")
    // public void exportToSensorExcel(HttpServletResponse response,
    // @RequestParam(value = "stationid", required = false) Integer stationid,
    // @RequestParam(value = "devicegroup", required = false) Integer devicegroup,
    // @RequestParam(value = "deviceaddr", required = false) Integer deviceaddr)
    // throws IOException {
    // Tabcaption record = new Tabcaption();
    // record.setStationid(stationid);
    // record.setDeviceaddr(deviceaddr);
    // record.setDevicegroup(devicegroup);
    // Workbook wb = tabcaptionServiceImpl.writeToExcelBySensorList(record);
    // OutputStream output = response.getOutputStream();
    // String fileName = "data.xlsx";
    // try {
    // fileName = URLEncoder.encode(fileName, "UTF-8");
    // } catch (UnsupportedEncodingException e) {
    // e.printStackTrace();
    // }
    // response.setHeader("Content-disposition",
    // "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
    // wb.write(output);
    // output.close();
    // }
}
